# mysite/asgi.py
import os
 
from channels.auth import AuthMiddlewareStack
from channels.routing import ProtocolTypeRouter, URLRouter
from django.core.asgi import get_asgi_application

import django
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "cucmarket.settings")
django.setup()

import chat.routing  
#error 记录 ： django.core.exceptions.AppRegistryNotReady: Apps aren't loaded yet. 项目无法启动

# 看到报错中log中有记录 
#  File "/home/w1nner/learn/softwareengine/Second-Market-master/cucmarket/./cucmarket/asgi.py", line 7, in <module>
#   import chat.routing

# 想到会不会是在  import chat.routing 前没有启动项目导致无法使用项目中的app
# 所以调换了项目中 import 的顺序 
# 成功解决问题
# oh oh oh oh oh oh ! 


application = ProtocolTypeRouter({
  "http": get_asgi_application(),
  "websocket": AuthMiddlewareStack(
        URLRouter(
            chat.routing.websocket_urlpatterns
        )
    ),
})